<?php
/**
 * Created by PhpStorm.
 * User: jhon
 * Date: 2018/6/14
 * Time: 10:01
 */

namespace app\admin\controller;
use think\Request;
use think\Db;

class Padsoftware extends Auto
{

    public function index(){
        return $this->fetch();
    }

    public function add(){
        $Request = Request::instance();
        $param = $Request->param();

        /*$fileparam = $this->uploadfile();
        if(!is_array($fileparam)){
            return 'error';
        }*/
        $data = [
            'name'=>$param['name'],
            'desc'=>$param['desc'],
            'pad_edition'=>$param['dev_edition'],
            'file_url'=>$param['file_url'],
            'create_time'=>date('Y-m-d H:i:s',time()),
        ];

      /*  $data = array_merge($fileparam,$data);
//        echo json_encode($data);return;*/
        $result  = Db::table('pad_software')->insert($data);
        if($result){
            $resultId = Db::table('pad_software')->getLastInsID();
            return $resultId;
        }else{//添加失败
            return 0;
        }
    }

    public function del(){
        $Request = Request::instance();
        $param = $Request->param();
        $ID = $param['ID'];
        $result  = Db::table('pad_software')->delete($ID);
        echo $result;
    }

    public function query(){
        $result  = Db::table('pad_software')
            ->select();
        $dataarr = [
            'data'=>$result
        ];
        echo json_encode($dataarr);
    }

    public function uploadfile(){
        $file = request()->file('filecontent');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'. DS .'padsoftware');
            if($info){
                return [
                    'file_ext'=>$info->getExtension(),
                    'file_url'=>$info->getSaveName(),
                    'file_name'=>$info->getFilename(),
                ];
            }else{
                // 上传失败获取错误信息
                return $file->getError();
            }
        }
    }

}